
!> \brief Contains some subroutines to print the collected data in several
!! formats
module mprint

   use data_collector

   implicit none

   !character (len=300), private

contains

   !============================================================================

   !> \brief Print data into a .csv file
   subroutine print_csv( nf, nv, nlen, filelist, varlist, vartype, varlabel)
      implicit none
      integer :: nf   !< number of files in the file list
      integer :: nv   !< number of variables to be read
      integer :: nlen !< String length
      character (len=nlen) :: filelist(nf)!< list of files
      character (len=nlen) :: varlist(nv) !< list of variales
      character (len=10)   :: vartype(nv) !< list of the type of the variables
      character (len=20)   :: varlabel(nv)!< list of the label of variables

      ! Inner variables

      integer :: i, j, res
      character (len=nlen) str


      open(17,file="./output_files/output_file.csv")
      !
      ! Extracting heading
      !
      do j = 1, nv

         if ( j == nv ) then

            write(17,"(A)") trim(adjustl(varlabel(j)))

         else

            write(17,"(A,'&')",advance='no') trim(adjustl(varlabel(j)))

         end if

      end do


      !
      ! Extracting data
      !

      do i = 1, nf

         do j = 1, nv

            call get_a_string_from_a_file(trim(adjustl(filelist(i))), &
               trim(adjustl(varlist(j))), str, res)

            ! Converting string to another string with format of vartype
            call converter(nlen, vartype(j), str)

            if ( j == nv ) then

               write(17,"(A)") trim(str)

            else

               write(17,"(A,'&')",advance='no') trim(str)

            end if

         end do

      end do

      close(17)

   end subroutine

   !============================================================================

   !> \brief Print data into a .tex file
   subroutine print_tex( nf, nv, nlen, filelist, varlist, vartype, varlabel)
      implicit none
      integer :: nf   !< number of files in the file list
      integer :: nv   !< number of variables to be read
      integer :: nlen !< String length
      character (len=nlen) :: filelist(nf)!< list of files
      character (len=nlen) :: varlist(nv) !< list of variales
      character (len=10)   :: vartype(nv) !< list of the type of the variables
      character (len=20)   :: varlabel(nv)!< list of the label of variables

      ! Inner variables

      integer :: i, j, res
      character :: char1
      character (len=nlen) str


      open(17,file="./output_files/output_file.tex")
      !
      ! Extracting heading
      !
      do j = 1, nv

         if ( j == nv ) then

            write(17,"(A,' \\')") trim(adjustl(varlabel(j)))

         else

            write(17,"(A,1x,'&',1x)",advance='no') trim(adjustl(varlabel(j)))

         end if

      end do


      !
      ! Extracting data
      !

      do i = 1, nf

         do j = 1, nv

            call get_a_string_from_a_file(trim(adjustl(filelist(i))), &
               trim(adjustl(varlist(j))), str, res)

            ! Converting string to another string with format of vartype
            call converter(nlen, vartype(j), str)

            char1 = vartype(j)

            if ( j == nv ) then
            
               if (char1 == "a" .or. char1 == "A") then

                  write(17,"(A,' \\')") trim(str)
               
               else
               
                  write(17,"('$',A,'$',' \\')") trim(str)

               end if

            else

               if (char1 == "a" .or. char1 == "A") then

                  write(17,"(A,1x,'&',1x)",advance='no') trim(str)

               else
               
                  write(17,"('$',A,'$',1x,'&',1x)",advance='no') trim(str)

               end if

            end if

         end do

      end do

      write(17,*)

      close(17)

   end subroutine

   !============================================================================

   !> \brief Print data into a .dat file
   subroutine print_dat( nf, nv, nlen, filelist, varlist, vartype, varlabel)
      implicit none
      integer :: nf   !< number of files in the file list
      integer :: nv   !< number of variables to be read
      integer :: nlen !< String length
      character (len=nlen) :: filelist(nf)!< list of files
      character (len=nlen) :: varlist(nv) !< list of variales
      character (len=10)   :: vartype(nv) !< list of the type of the variables
      character (len=20)   :: varlabel(nv)!< list of the label of variables

      ! Inner variables

      integer :: i, j, res
      character (len=nlen) str


      open(17,file="./output_files/output_file.dat")
      !
      ! Extracting heading
      !
      do j = 1, nv

         if ( j == nv ) then

            write(17,"(1X,A)") trim(adjustl(varlabel(j)))

         else if ( j == 1 ) then

            write(17,"('# ', A)",advance='no') trim(adjustl(varlabel(j)))

         else

            write(17,"(1X,A)",advance='no') trim(adjustl(varlabel(j)))

         end if

      end do


      !
      ! Extracting data
      !

      do i = 1, nf

         do j = 1, nv

            call get_a_string_from_a_file(trim(adjustl(filelist(i))), &
               trim(adjustl(varlist(j))), str, res)

            ! Converting string to another string with format of vartype
            call converter(nlen, vartype(j), str)

            if ( j == nv ) then

               write(17,"(1X,A)") trim(str)

            else

               write(17,"(1X,A)",advance='no') trim(str)

            end if

         end do

      end do

      close(17)

   end subroutine

   !============================================================================

   !> \brief Converts str to the format specified by vartype
   subroutine converter(nlen, vartype, str)
      implicit none
      integer, intent(in) :: nlen !< String length
      character (len=10), intent(in) :: vartype !< list of the type of the variables
      character (len=nlen), intent(inout) :: str !< Converted string

      ! Inner variables

      character(1) :: vtype1
      character(3) :: vtype3
      integer :: iaux
      real(8) :: raux

      integer :: IO

      vtype1 = trim(adjustl(vartype))
      
      if (vtype1 == "1") then
      
         vtype3 = trim(adjustl(vartype))
         
      else
      
         vtype3 = vtype1
      
      end if

      select case (vtype3)

         case ("i","I")

            read(str,*, iostat=IO ) iaux

            if ( IO /= 0 ) then

               str = " "

            else

               write(str, '(' // trim(adjustl(vartype)) // ')') iaux

            end if

         case ("f", "F", "e", "E", "1pe", "1PE")

            read(str,*, iostat=IO ) raux

            if ( IO /= 0 ) then

               str = " "

            else

               write(str, '(' // trim(adjustl(vartype)) // ')') raux

            end if

         case ("a", "A")

            write(str, '(' // trim(adjustl(vartype)) // ')') trim(adjustl(str))

         case default

            write(*,"(A)") "Unknown format. Stopping..."
            stop

      end select

   end subroutine

   !============================================================================

end module
